package top.whk0710.leetcode;

public class Solution {

	public static TreeNode remove(TreeNode root, int value){
		
		if(root == null) return null;
		if(value < root.val){
			root.left = remove(root.left,value);
		}else if(value > root.val){
			root.right = remove(root.right,value);
		}else{
			if(root.left == null || root.right == null){
				root = root.left == null ? root.right : root.left;
			}
			else{
				TreeNode curr = root.right;
				while(curr.left != null){
					curr = curr.left;
				}
				root.val = curr.val;
				root.right = remove(root.right,curr.val);
			}
		}
		return root;
	}
}

class TreeNode{
	
	int val;
	TreeNode left;
	TreeNode right;
	
	public TreeNode(int val){
		this.val = val;
	}
}